/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.RecommendConfig;
import com.boxin.ims.modules.wechat.dao.RecommendConfigDao;

/**
 * 三级推广配置文件Service
 * @author zhangshanshan
 * @version 2016-03-14
 */
@Component
@Transactional(readOnly = true)
public class RecommendConfigService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(RecommendConfigService.class);
	
	@Autowired
	private RecommendConfigDao recommendConfigDao;
	
	public RecommendConfig get(Long id) {
		return recommendConfigDao.findOne(id);
	}
	
	public RecommendConfig getByUserId(Long userId){
		return recommendConfigDao.getByUserId(userId);
	}
	
	public Page<RecommendConfig> find(Page<RecommendConfig> page, RecommendConfig recommendConfig) {
		DetachedCriteria dc = recommendConfigDao.createDetachedCriteria();
		if ( recommendConfig.getUserId() != null && recommendConfig.getUserId()>0){
			dc.add(Restrictions.eq("userId", recommendConfig.getUserId()));
		}
		if (StringUtils.isNotEmpty(recommendConfig.getName())){
			dc.add(Restrictions.like("name", "%"+recommendConfig.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return recommendConfigDao.find(page, dc);
	}
	
	public List<RecommendConfig> find(RecommendConfig recommendConfig){
		DetachedCriteria dc = recommendConfigDao.createDetachedCriteria();
		if ( recommendConfig.getUserId() != null && recommendConfig.getUserId()>0){
			dc.add(Restrictions.eq("userId", recommendConfig.getUserId()));
		}
		if (StringUtils.isNotEmpty(recommendConfig.getName())){
			dc.add(Restrictions.like("name", "%"+recommendConfig.getName()+"%"));
		}
		if(StringUtils.isNotBlank(recommendConfig.getIsOpen())){
			dc.add(Restrictions.eq("isOpen", recommendConfig.getIsOpen()));
		}
		if(StringUtils.isNotBlank(recommendConfig.getType())){
			dc.add(Restrictions.eq("type", recommendConfig.getType()));
		}
		dc.addOrder(Order.desc("id"));
		return recommendConfigDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public void save(RecommendConfig recommendConfig) {
		
		recommendConfig.setMessage(StringEscapeUtils.unescapeHtml4(recommendConfig.getMessage()));
		recommendConfig.setScoreMessage(StringEscapeUtils.unescapeHtml4(recommendConfig.getScoreMessage()));
		recommendConfig.setParentMessage(StringEscapeUtils.unescapeHtml4(recommendConfig.getParentMessage()));
		recommendConfig.setRepetitionMessage(StringEscapeUtils.unescapeHtml4(recommendConfig.getRepetitionMessage()));
		
		recommendConfig.setNewHelp(StringEscapeUtils.unescapeHtml4(recommendConfig.getNewHelp()));
		recommendConfigDao.save(recommendConfig);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		recommendConfigDao.deleteById(id);
	}
	
	
	/** 根据userId将活动关闭  */
	public int closeAllByUserId(Long userId){
		return recommendConfigDao.closeAllByUserId(userId);
	}
	
	/** 根据id将活动开启  */
	public int openRecommendConfigById(Long id){
		return recommendConfigDao.openRecommendConfigById(id);
	}
	
}
